package com.example.myproject.controller;

import com.example.myproject.common.ResponseData;
import com.example.myproject.dto.bounty.*;
import com.example.myproject.security.JwtAuthenticationFilter;
import com.example.myproject.service.BountyService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/bounty")
@AllArgsConstructor
public class BountyController {

    private final BountyService bountyService;
    // 发布悬赏
    @PostMapping("/publish")
    public ResponseEntity<?> publishBounty(@RequestBody BountyDTO bountyDTO, @AuthenticationPrincipal JwtAuthenticationFilter.UserPrincipal principal) {
        return ResponseEntity.ok(bountyService.publishBounty(principal.getId(),bountyDTO));
    }

    // 追加悬赏
    @PostMapping("/addamount")
    public ResponseEntity<?> addBountyAmount(@RequestBody BountyContributionDTO dto, @AuthenticationPrincipal JwtAuthenticationFilter.UserPrincipal principal) {
        return ResponseEntity.ok(bountyService.addContribution(principal.getId(), dto));
    }

    //取消悬赏
    @PostMapping("/cancel")
    public ResponseEntity<?> cancelBounty(@RequestBody CancelBountyRequest request,
                                          @AuthenticationPrincipal JwtAuthenticationFilter.UserPrincipal principal) {
        return ResponseEntity.ok(bountyService.cancelBounty(request.getBountyId(), principal.getId()));
    }


    // 查看我的悬赏
    @GetMapping("/mybounties")
    public ResponseEntity<?> getMyBounties(@AuthenticationPrincipal JwtAuthenticationFilter.UserPrincipal principal) {
        return ResponseEntity.ok(bountyService.getMyBounties(principal.getId()));
    }

    // 查看我追加过的悬赏
    @GetMapping("/mycontributions")
    public ResponseEntity<?> getMyContributions(@AuthenticationPrincipal JwtAuthenticationFilter.UserPrincipal principal)  {
        return ResponseEntity.ok(bountyService.getMyContributions(principal.getId()));
    }

    //查看我的响应
    @GetMapping("/mysubmissions")
    public ResponseEntity<?> getMySubmissions(
            @AuthenticationPrincipal JwtAuthenticationFilter.UserPrincipal principal) {
        return ResponseEntity.ok(bountyService.getMySubmissions(principal.getId()));
    }

    // 查看所有悬赏（可分页、可加筛选条件）
    @GetMapping("/all")
    public ResponseEntity<?> getAllBounties() {
        return ResponseEntity.ok(bountyService.getAllBounties());
    }

    // 确认悬赏
    @PostMapping("/approve")
    public ResponseEntity<?> approveSubmission(
            @RequestBody ApproveSubmissionRequest request,
            @AuthenticationPrincipal JwtAuthenticationFilter.UserPrincipal principal) {
        return ResponseEntity.ok(bountyService.approveSubmission(request.getSubmissionId(), principal.getId()));
    }


    // 拒绝悬赏（附带理由）
    @PostMapping("/reject")
    public ResponseEntity<?> rejectSubmission(@RequestBody RejectSubmissionRequest request,
                                              @AuthenticationPrincipal JwtAuthenticationFilter.UserPrincipal principal) {
        return ResponseEntity.ok(bountyService.rejectSubmission(request.getSubmissionId(), request.getReason(), principal.getId()));
    }
    //响应悬赏
    @PostMapping("/upload-file")
    public ResponseEntity<?> uploadFromFile(
            @RequestParam(value = "bounty_id", required = true) Long bountyId,
            @RequestPart("file") MultipartFile file,
            @AuthenticationPrincipal JwtAuthenticationFilter.UserPrincipal principal
    ) {
        Long torrentId =bountyService.submitFile(principal.getId(),bountyId, file);
        return ResponseEntity.ok(ResponseData.success(torrentId));
    }

    //响应悬赏
    @PostMapping("/upload-torrent")
    public ResponseEntity<?> uploadFromTorrentFile(
            @RequestParam(value = "bounty_id", required = true) Long bountyId,
            @RequestPart("file") MultipartFile file,
            @AuthenticationPrincipal JwtAuthenticationFilter.UserPrincipal principal
    ) {
        Long torrentId = bountyService.submitTorrent(principal.getId(),bountyId, file);
        return ResponseEntity.ok(ResponseData.success(torrentId));
    }

    //仲裁部分
    //仲裁列表
    @GetMapping("/arbitration/all")
    public ResponseEntity<?> getArbitrationList() {
        return ResponseEntity.ok(ResponseData.success(bountyService.getSubmissionsForArbitration()));
    }

    //同意仲裁，即响应悬赏的种子不满足要求
    @PostMapping("/arbitration/approve")
    public ResponseEntity<?> approveArbitration(@RequestBody ApproveArbitrationRequest request) {
        return ResponseEntity.ok(bountyService.approveByArbitration(request.getSubmissionId()));
    }

    // 仲裁拒绝
    @PostMapping("/arbitration/reject")
    public ResponseEntity<?> rejectArbitration(@RequestBody RejectArbitrationRequest request) {
        return ResponseEntity.ok(bountyService.rejectByArbitration(request.getSubmissionId()));
    }


}
